package controller.login;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
import controller.dao.Dao_User;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("login");
		String pass = request.getParameter("pass");
		Dao_User daoe = new Dao_User();
		User user = daoe.log(pass, name);
		if(user != null){
			HttpSession logUser = request.getSession();
			String[] types = user.getProfileType().split(",");
			logUser.setAttribute("login", name);
			logUser.setAttribute("profiles", types);
			response.sendRedirect("../admin/controller.ja");
		}
		else{
			RequestDispatcher out = request.getRequestDispatcher("/admin/erroLogin.jsp");
			out.forward(request, response);
		}
	}
}
